package org.bonorsoft.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.modules.system.entity.SysUser;
import org.bonorsoft.modules.system.entity.UserAudit;
import org.bonorsoft.modules.system.service.ISysUserService;
import org.bonorsoft.modules.system.service.IUserAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

@Slf4j
@Controller
@RequestMapping("/system/userAudit")
public class UserAuditController extends BonorController<UserAudit, IUserAuditService> {
    private String prefix = "system/userAudit";

    @Autowired
    private IUserAuditService userAuditService;
    @Autowired
    private ISysUserService sysUserService;


    /**
     * 首页
     */
    @RequiresPermissions("system:userAudit:view")
    @GetMapping()
    public String index() {
        return prefix + "/userAudit";
    }

    /**
     * 分页列表查询
     */
    //@AutoLog(value = "群众注册信息-分页列表查询")
    @RequiresPermissions("system:userAudit:list")
    @GetMapping(value = "/list")
    @ResponseBody
    public Result<?> queryPageList(UserAudit userAudit,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<UserAudit> queryWrapper = QueryGenerator.initQueryWrapper(userAudit, req.getParameterMap());
        Page<UserAudit> page = new Page<UserAudit>(pageNo, pageSize);
        IPage<UserAudit> pageList = userAuditService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "群众注册信息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        userAuditService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "群众注册信息-批量删除")
    @RequiresPermissions("system:userAudit:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    @ResponseBody
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.userAuditService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 编辑页面
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map) {
        UserAudit userAudit = userAuditService.getById(id);
        map.put("userAudit", userAudit);
        return prefix + "/edit";
    }

    /**
     * 编辑
     */
    @AutoLog(value = "群众注册信息-编辑")
    @RequiresPermissions("system:userAudit:edit")
    @PostMapping(value = "/edit")
    @ResponseBody
    public Result<?> edit(@Validated UserAudit userAudit) {
        userAuditService.updateById(userAudit);
        return Result.ok("编辑成功!");
    }

    /**
     * 详细
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap map) {
        UserAudit userAudit = userAuditService.getById(id);
        map.put("userAudit", userAudit);
        return prefix + "/detail";
    }


    /**
     * 审核通过（并删除审核表数据）
     */
    @PostMapping(value = "/adopt/{id}")
    @ResponseBody
    public Result<?> adopt(@PathVariable("id") String id) {
        UserAudit userAudit = userAuditService.getById(id);
        SysUser user = new SysUser();
        user.setUsername(userAudit
                .getUsername())
                .setRealname(userAudit.getRealname())
                .setSex(userAudit.getSex())
                .setPhone(userAudit.getPhone())
                .setPassword(userAudit.getPassword())
                .setRemark(userAudit.getRemark());
        String[] ordinary = new String[]{"0bddd649cbaf00103cc3ea3f18d2ffd3"};//群众权限
        sysUserService.add(user, ordinary);
        userAuditService.removeById(id);
        return Result.ok("审核通过成功!");
    }

}
